//
//  KHHRadioViewController.m
//  Travel
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHRadioViewController.h"
#import "CXNetHandle.h"
#import "KHHMuscieModel.h"
#import "UIImageView+WebCache.h"
#import "KHHPlayViewController.h"
#import "KHHMuscieTableViewCell.h"

@interface KHHRadioViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation KHHRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"音悦台";
    [self getData];
    [self creatTableView];
    
    //菊花
    [CXLoadMBProgress loadMBProgress:self.view];
    
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[KHHMuscieTableViewCell class] forCellReuseIdentifier:@"cell"];
}
#pragma mark - 获取数据
- (void)getData
{
    self.dataArr = [NSMutableArray array];
    NSString *url = @"http://mobile.ximalaya.com/mobile/others/ca/album/track/321797/true/1/30?device=iPhone";
    [CXNetHandle getUrl:url cookie:nil result:^(id result) {
        
        //移除菊花
        [CXLoadMBProgress removeMBProgress:self.view];
        
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:result[@"tracks"][@"list"]];
        self.dataArr = [KHHMuscieModel transformWithArray:tempArr];
        [self.tableView reloadData];
    }];
    [self.tableView reloadData];
    
}

#pragma mark - cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
#pragma mark - 重用cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KHHMuscieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KHHPlayViewController *pVC = [KHHPlayViewController ShareKHHPlayerViewController];
    pVC.model = self.dataArr[indexPath.row];
    pVC.indexRow = indexPath.row; // 点击列表row
    pVC.allMusicArray = self.dataArr; // 总音乐列表
    [self.navigationController pushViewController:pVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
